package com.jshare.action;

import java.sql.Timestamp;
import java.util.Date;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import com.jshare.entity.News;
import com.jshare.service.NewsService;
import com.jshare.util.Pager;
import com.opensymphony.xwork2.ActionSupport;

public class NewsAction extends ActionSupport {

	@Resource(name="newsService")
	private NewsService newsService;
	private News news;
	private Pager newsPager;
	public String edit()
	{
		return "edit";
	}
	public String addNews()
	{
		news.setPostdate(new Timestamp(new Date().getTime()));
		newsService.addAnews(news);
		newsPager = newsService.getPageData(2, 1);
		return "listNews";
	}
	public String listNews()
	{
		newsPager = newsService.getPageData(2,1);
		return "listNews";
	}
	public String getPagerNews()
	{
		//可以自己传pageSize.我们人为认定了pageSize
		HttpServletRequest request= ServletActionContext.getRequest();
		int pagenow=Integer.parseInt(request.getParameter("pagenow"));
		if(pagenow<=0)
		{
			pagenow = 1;
		}
		newsPager = newsService.getPageData(3, pagenow);
		return "listRefer";
	}
	public String listRefer()
	{
		newsPager = newsService.getPageData(3, 1);
		return "listRefer";
	}
	public String deleteNews()
	{
		HttpServletRequest request= ServletActionContext.getRequest();
		int id=Integer.parseInt(request.getParameter("id"));
		if(newsService.isExist(id))
		{
			newsService.deleteAnews(id);
		}
		newsPager = newsService.getPageData(2, 1);
		return "listRefer";
	}
	public String look()
	{
		HttpServletRequest request= ServletActionContext.getRequest();
		int id=Integer.parseInt(request.getParameter("id"));
		System.out.println("ID" + id);
		if(newsService.isExist(id))
		{
			news = newsService.getNewsById(id);
		}
		return "look";
	}
	public NewsService getNewsService() {
		return newsService;
	}
	public void setNewsService(NewsService newsService) {
		this.newsService = newsService;
	}
	public News getNews() {
		return news;
	}
	public void setNews(News news) {
		this.news = news;
	}
	public Pager getNewsPager() {
		return newsPager;
	}
	public void setNewsPager(Pager newsPager) {
		this.newsPager = newsPager;
	}
}
